package com.zhoug.player3.core.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.TextureView;

import com.zhoug.player3.core.ZPlayer;
import com.zhoug.player3.core.utils.VideoSizeHelper;


/**
 * @Author: zhoug
 * @Date: 2023/1/19
 * @Description:
 */
public class VideoRatioTextureView extends TextureView {
    private VideoSizeHelper mVideoSizeHelper;

    public VideoRatioTextureView(Context context) {
        super(context);
        init();
    }


    public VideoRatioTextureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    public VideoRatioTextureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        if (mVideoSizeHelper == null) {
            mVideoSizeHelper = new VideoSizeHelper(this);
        }
    }

    public void setRatio(float ratio) {
        mVideoSizeHelper.setRatio(ratio);
    }

    /**
     * 设置显示模式
     * {@link ZPlayer#RENDER_MODE_ADJUST_RESOLUTION} 自适应模式：视频画面将等比例缩放，会居中显示，此模式可能会有黑边
     * {@link ZPlayer#RENDER_MODE_FULL_FILL_SCREEN} 平铺模式：视频画面将会按照比例铺满屏幕，多余部分会被裁减掉，此模式下不会有黑边。
     *
     * @param renderMode
     */
    public void setRenderMode(int renderMode) {
        mVideoSizeHelper.setRenderMode(renderMode);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int[] measureSpecs = mVideoSizeHelper.onMeasure();
        if (measureSpecs != null) {
            super.onMeasure(measureSpecs[0], measureSpecs[1]);
        }
    }
}
